package com.dateview.test.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dateview.test.R;

import java.util.List;

/**
 * Author: dengdayi
 * Date:2016/1/11 16:04
 * Description:
 */
public class GirdDateAdapter extends BaseAdapter {
    private List<String> mDateStr;

    private Context mContext;

    private LayoutInflater mInflater;

    public GirdDateAdapter(Context context, List<String> mDateStr) {
        this.mDateStr = mDateStr;
        this.mContext = context;

        if (mInflater == null) {
            mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    }

    @Override
    public int getCount() {
        return mDateStr.size();
    }

    @Override
    public Object getItem(int position) {
        return mDateStr.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder mViewHolder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.group_list_item, null);
            mViewHolder = new ViewHolder();
            mViewHolder.mTextView = (TextView) convertView.findViewById(R.id.tv_text);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        mViewHolder.mTextView.setText(mDateStr.get(position));

        return convertView;
    }

    private class ViewHolder {
        TextView mTextView;
    }

    /**
     * 重写 isEnabled  ，当item为null 或  “”是不可点击
     * @param position
     * @return
     */
    @Override
    public boolean isEnabled(int position) {

        if (mDateStr == null) {
            return false;
        }

        String str = mDateStr.get(position);
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return super.isEnabled(position);
    }
}
